﻿Imports System.Text.RegularExpressions

Namespace Helpers

    Public Class BarcodeHelper

        Public Const INVALID_MESSAGE As String = "Ongeldige barcode. Deze moet bestaan uit 12 of 13 cijfers."

        Private Sub New() ' zorg dat er geen publieke constructor is
        End Sub

        Public Shared Function IsValid(ByVal code As String) As Boolean
            If (code.Length = 0) Then Return False
            If (Not Regex.IsMatch(code, "^\d{12,13}$")) Then Return False
            ' evt. ook check-digit controleren
            Return True
        End Function

        Public Shared Function GetNextBarcode(ByVal huidigeBarcode As String) As String
            Dim nHuidige As Int64 = Convert.ToInt64(huidigeBarcode.Substring(0, huidigeBarcode.Length - 1))
            Dim nieuweBarcode As String = (nHuidige + 1).ToString()
            nieuweBarcode = nieuweBarcode & CalculateChecksumDigit(nieuweBarcode)
            Return nieuweBarcode
        End Function

        Public Shared Function CalculateChecksumDigit(ByVal sTemp As String) As String
            'Dim sTemp As String = CountryCode + ManufacturerCode + ProductCode
            Dim iSum As Integer = 0

            ' Calculate the checksum digit here.
            For i As Integer = 1 To sTemp.Length
                Dim iDigit As Integer = Convert.ToInt32(sTemp.Substring(sTemp.Length - i, 1))
                ' This appears to be backwards but the EAN-13 checksum must be calculated
                ' this way to be compatible with UPC-A.
                If i Mod 2 = 1 Then
                    ' odd 
                    iSum += iDigit * 3
                Else
                    ' even
                    iSum += iDigit * 1
                End If
            Next
            Dim iCheckSum As Integer = (10 - (iSum Mod 10)) Mod 10
            Return iCheckSum.ToString()
        End Function

        Public Shared Function FixBarcode(ByVal invoer As String) As String
            ' Barcode van 14 cijfers die begint met 0540016 => 0 eraf strippen
            Dim pos As Integer = invoer.IndexOf("*0540016")
            If (pos >= 0 AndAlso invoer.Length - pos = 15) Then
                invoer = invoer.Replace("*0", "*")
            End If
            If (invoer.Length = 14 AndAlso invoer.StartsWith("0540016")) Then
                invoer = invoer.Substring(1)
            End If
            Return invoer
        End Function

        Public Shared Function CheckValidBarcode(ByVal invoer As String, ByVal laatNegatiefAantalToe As Boolean) As Boolean
            ' optioneel minteken, 1 tot 3 cijfers (het aantal) gevolgd door *
            ' vervolgens 8 tot 14 cijfers (de barcode)
            Dim patroon As String = "^(-?[123456789]\d{0,2}\*)?\d{8,13}$"
            If (Not laatNegatiefAantalToe) Then
                ' minteken niet toelaten
                patroon = "^([123456789]\d{0,2}\*)?\d{8,13}$"
            End If
            Dim regex As New Regex(patroon)
            Dim trimmedText As String = invoer.Trim(" "c)
            Return regex.IsMatch(trimmedText)
        End Function

    End Class

End Namespace
